﻿using ApprovalTests;
using ApprovalTests.Reporters;
using DZParallelLib.Test.Lib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace DZParallelLib.Test
{
    /// <summary>
    /// WARNING::::::
    /// The diff reporter is good for seeing what is different about the XML files, however, there is a UTF character
    /// that the serializer writes out that the diff reporter kills when it does the copy... the result is you can't do the matching
    /// in the diff reporter! So, once you've seen the differences, past the new command line into a cmd window and execute it to update
    /// the tests.
    /// </summary>
    [TestClass]
    public class RenderDZCollectionFromImagesTest
    {
        [TestInitialize]
        public void TestInit()
        {
            UtilsForTest.SetupTest();
        }

        [TestMethod]
        public void TestCTor()
        {
            var c = new RenderDZCollectionFromImages();
            Assert.IsNotNull(c);
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public async Task TestCreate1FromNothing()
        {
            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var output = new FileInfo(@".\dz.xml");
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var updated = await c.WriteNewCollectionFile(new FileInfo[] { i1 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);
            Assert.IsNotNull(updated, "Return from render images");
            Assert.AreEqual(1, updated.Count(), "Number of images that were updated");
            Assert.AreEqual((ulong)0, updated.First(), "Image number");
            Assert.IsTrue(output.Exists, "Output file");
            Approvals.Verify(output);
        }

        [TestMethod]
        [DeploymentItem("DZImageSmall", "DZImageSmall")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public async Task TestCreateSmallFromNothing()
        {
            // Check rendering an image that is smaller than 256x256.

            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImageSmall\sc\vnpzyair.xml");
            var output = new FileInfo(@".\dz.xml");
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var updated = await c.WriteNewCollectionFile(new FileInfo[] { i1 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);
            Assert.IsNotNull(updated, "Return from render images");
            Assert.AreEqual(1, updated.Count(), "Number of images that were updated");
            Assert.AreEqual((ulong)0, updated.First(), "Image number");
            Assert.IsTrue(output.Exists, "Output file");
            Approvals.Verify(output);
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public async Task TestCreate2FromNothing()
        {
            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var i2 = i1.GenerateNewDZFile("vnpzyair2.xml");
            var output = new FileInfo(@".\dz.xml");
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var updated = await c.WriteNewCollectionFile(new FileInfo[] { i1, i2 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);
            Assert.IsNotNull(updated, "Return from render images");
            Assert.AreEqual(2, updated.Count(), "Number of images that were updated");
            Assert.AreEqual((ulong)0, updated.First(), "Image number");
            Assert.AreEqual((ulong)1, updated.Skip(1).First(), "Image number");
            Assert.IsTrue(output.Exists, "Output file");
            Approvals.Verify(output);
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public async Task TestCreate1Twice()
        {
            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var output = new FileInfo(@".\dz.xml");
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var updated = await c.WriteNewCollectionFile(new FileInfo[] { i1 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);

            updated = await c.WriteNewCollectionFile(new FileInfo[] { i1 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);

            Assert.AreEqual(0, updated.Count(), "# Updated Images Needed");

            output.Refresh();
            Assert.IsTrue(output.Exists, "Output file");
            Approvals.Verify(output);
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public async Task LookForSkippedIDs()
        {
            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var output = new FileInfo(@".\dz.xml");
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var updated = await c.WriteNewCollectionFile(new FileInfo[] { i1 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);

            var i2 = i1.GenerateNewDZFile("vnpzyair2.xml");

            updated = await c.WriteNewCollectionFile(new FileInfo[] { i1, i2 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);

            Assert.AreEqual(1, updated.Count(), "# Updated Images Needed");
            Assert.AreEqual((ulong)1, updated.First(), "Updated image id");

            output.Refresh();
            Assert.IsTrue(output.Exists, "dz file");

            // Make sure both are there, and they are in order from 0 to 1.
            Approvals.Verify(output);

        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public async Task DeletedImageShouldGoAway()
        {
            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var i2 = i1.GenerateNewDZFile("vnpzyair2.xml");

            var output = new FileInfo(@".\dz.xml");
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var updated = await c.WriteNewCollectionFile(new FileInfo[] { i1, i2 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);

            updated = await c.WriteNewCollectionFile(new FileInfo[] { i2 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);

            Assert.AreEqual(0, updated.Count(), "# Updated Images Needed");

            // Make sure both the old and the new guy are here!
            Approvals.Verify(output);
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public async Task AddedImageShouldSkipDeletedImage()
        {
            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var i2 = i1.GenerateNewDZFile("vnpzyair2.xml");

            var output = new FileInfo(@".\dz.xml");
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var updated = await c.WriteNewCollectionFile(new FileInfo[] { i1, i2 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);

            var i3 = i1.GenerateNewDZFile("vnpzyair3.xml");

            updated = await c.WriteNewCollectionFile(new FileInfo[] { i1, i3 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);

            Assert.AreEqual(1, updated.Count(), "# Updated Images Needed");
            Assert.AreEqual((ulong)2, updated.First(), "Updated image id");
        }


        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public async Task NullArguments()
        {
            var c = new RenderDZCollectionFromImages();
            await c.WriteNewCollectionFile(null, null, null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public async Task BadInputFilePointer()
        {
            var c = new RenderDZCollectionFromImages();
            await c.WriteNewCollectionFile(new FileInfo[] { new FileInfo(@".\bogus.xml") }, f => "hi", new FileInfo("dude.xml"));
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public void RenderCollectionOneImage()
        {
            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var output = new FileInfo(@".\DZImage1\dz.xml"); // Note rendered image locations are important for this test!
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var indicies = c.RenderCollection(new FileInfo[] { i1 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;
            Assert.IsNotNull(indicies, "Return from render images");
            Assert.AreEqual(1, indicies.Count(), "Number of images that were rendered");
            Assert.AreEqual((ulong)0, indicies.First(), "Image number");
            output.Refresh();
            Assert.IsTrue(output.Exists, "Output file");
            Assert.IsTrue(Directory.Exists(Path.Combine(output.Directory.FullName, "dz_files")), "the main sub image directory");
            Assert.IsTrue(Directory.Exists(Path.Combine(output.Directory.FullName, "dz_files", "7")), "the 7 sub image directory");
            Assert.IsTrue(Directory.Exists(Path.Combine(output.Directory.FullName, "dz_files", "0")), "the 0 sub image directory");
            Assert.IsTrue(Directory.Exists(Path.Combine(output.Directory.FullName, "dz_files", "8")), "the 8 sub image directory");
            Assert.IsFalse(Directory.Exists(Path.Combine(output.Directory.FullName, "dz_files", "9")), "the 9 sub image directory");

            Approvals.Verify(output);
        }

        [TestMethod]
        public async Task RenderCollectionNoImages()
        {
            var output = new FileInfo(@".\DZImage1\dz.xml"); // Note rendered image locations are important for this test!
            if (output.Exists)
                output.Delete();
            output.Refresh();

            var c = new RenderDZCollectionFromImages();
            var indicies = await c.RenderCollection(new FileInfo[] { },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);
            Assert.IsNotNull(indicies, "Return from render images");
            Assert.AreEqual(0, indicies.Count(), "Number of images that were rendered");
            output.Refresh();
            Assert.IsTrue(output.Exists, "Output file");
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImageRenderCollectionTwoImages")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public void RenderCollectionTwoImages()
        {
            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImageRenderCollectionTwoImages\sc\vnpzyair.xml");
            var i2 = i1.GenerateNewDZFile("vnpzyair2.xml");
            var output = new FileInfo(@".\DZImageRenderCollectionTwoImages\dz.xml"); // Note rendered image locations are important for this test!
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var indicies = c.RenderCollection(new FileInfo[] { i1, i2 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;
            Assert.IsNotNull(indicies, "Return from render images");
            Assert.AreEqual(2, indicies.Count(), "Number of images that were rendered");
            Assert.AreEqual((ulong)0, indicies.First(), "Image number");
            Assert.AreEqual((ulong)1, indicies.Skip(1).First(), "Image number");
            output.Refresh();
            Assert.IsTrue(output.Exists, "Output file");
            Assert.IsTrue(Directory.Exists(Path.Combine(output.Directory.FullName, "dz_files")), "the main sub image directory");
            Assert.IsTrue(Directory.Exists(Path.Combine(output.Directory.FullName, "dz_files", "8")), "the 7 sub image directory");
            Assert.IsTrue(Directory.Exists(Path.Combine(output.Directory.FullName, "dz_files", "0")), "the 0 sub image directory");
            Assert.AreEqual(2, Directory.EnumerateFiles(Path.Combine(output.Directory.FullName, "dz_files", "8")).Count(), "# of images at top level");

            Approvals.Verify(output);
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public void RenderCollectionWithSkippedImage()
        {
            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var i2 = i1.GenerateNewDZFile("vnpzyair2.xml");

            var output = new FileInfo(@".\DZImage1\dz.xml"); // Relative output directory location matters
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var renderedids = c.RenderCollection(new FileInfo[] { i1, i2 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            var i3 = i1.GenerateNewDZFile("vnpzyair3.xml");

            renderedids = c.RenderCollection(new FileInfo[] { i1, i3 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            Assert.AreEqual(2, renderedids.Count(), "Number of images that were rendered");
            Assert.AreEqual((ulong)0, renderedids.First(), "First image ID");
            Assert.AreEqual((ulong)2, renderedids.Skip(1).First(), "First image ID");

            output.Refresh();
            Assert.IsTrue(output.Exists, "dz file");

            // THe output file should contain all three, in order.
            Approvals.Verify(output);
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public void RenderCollectionWithSkippedAndReturnedImage()
        {
            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var i2 = i1.GenerateNewDZFile("vnpzyair2.xml");

            var output = new FileInfo(@".\DZImage1\dz.xml"); // Relative output directory location matters
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var renderedids = c.RenderCollection(new FileInfo[] { i1 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            renderedids = c.RenderCollection(new FileInfo[] { i2 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            renderedids = c.RenderCollection(new FileInfo[] { i1 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            Assert.AreEqual(1, renderedids.Count(), "Number of images that were rendered");

            output.Refresh();
            Assert.IsTrue(output.Exists, "dz file");

            // THe output file should contain all three, in order.
            Approvals.Verify(output);
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public void RenderCollectionWithLotsOfSkippedImages()
        {
            var c = new RenderDZCollectionFromImages();

            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var list1 = (from r in Enumerable.Range(0, 10)
                         select i1.GenerateNewDZFile(string.Format("vnpzyair{0}.xml", r))).ToArray();
            var list2 = (from r in Enumerable.Range(11, 15)
                         select i1.GenerateNewDZFile(string.Format("vnpzyair{0}.xml", r))).ToArray();

            var output = new FileInfo(@".\DZImage1\dz.xml"); // Relative output directory location matters
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var renderedids = c.RenderCollection(list1,
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            renderedids = c.RenderCollection(list2,
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            Assert.AreEqual(15, renderedids.Count(), "Number of images that were rendered");

            output.Refresh();
            Assert.IsTrue(output.Exists, "dz file");

            // THe output file should contain all 10, in order.
            Approvals.Verify(output);
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public void RenderCollectionWith3SkipUpdates()
        {
            var c = new RenderDZCollectionFromImages();

            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var list1 = (from r in Enumerable.Range(0, 10)
                         select i1.GenerateNewDZFile(string.Format("vnpzyair{0}.xml", r))).ToArray();
            var list2 = (from r in Enumerable.Range(11, 15)
                         select i1.GenerateNewDZFile(string.Format("vnpzyair{0}.xml", r))).ToArray();
            var list3 = (from r in Enumerable.Range(25, 12)
                         select i1.GenerateNewDZFile(string.Format("vnpzyair{0}.xml", r))).ToArray();

            var output = new FileInfo(@".\DZImage1\dz.xml"); // Relative output directory location matters
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var renderedids = c.RenderCollection(list1,
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            renderedids = c.RenderCollection(list2,
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            renderedids = c.RenderCollection(list3,
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            Assert.AreEqual(12, renderedids.Count(), "Number of images that were rendered");

            output.Refresh();
            Assert.IsTrue(output.Exists, "dz file");

            // THe output file should contain all 10, in order.
            Approvals.Verify(output);
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public void RenderCollectionWith3OutOfOrderUpdates()
        {
            var c = new RenderDZCollectionFromImages();

            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var list1 = (from r in Enumerable.Range(0, 10)
                         select i1.GenerateNewDZFile(string.Format("vnpzyair{0}.xml", r))).ToArray();
            var list2 = (from r in Enumerable.Range(11, 15)
                         select i1.GenerateNewDZFile(string.Format("vnpzyair{0}.xml", r))).ToArray();
            var list3 = (from r in Enumerable.Range(25, 12)
                         select i1.GenerateNewDZFile(string.Format("vnpzyair{0}.xml", r))).ToArray();

            var output = new FileInfo(@".\DZImage1\dz.xml"); // Relative output directory location matters
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var renderedids = c.RenderCollection(list1,
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            renderedids = c.RenderCollection(list2,
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            renderedids = c.RenderCollection(list3.Concat(list1.Take(2)),
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            Assert.AreEqual(14, renderedids.Count(), "Number of images that were rendered");

            output.Refresh();
            Assert.IsTrue(output.Exists, "dz file");

            // THe output file should contain all 10, in order.
            Approvals.Verify(output);
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        [UseReporter(typeof(DiffReporter), typeof(ClipboardReporter))]
        public void RenderCollectionWithRemovedImage()
        {
            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var i2 = i1.GenerateNewDZFile("vnpzyair2.xml");

            var output = new FileInfo(@".\DZImage1\dz.xml"); // Relative output directory location matters
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var renderedids = c.RenderCollection(new FileInfo[] { i1, i2 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            renderedids = c.RenderCollection(new FileInfo[] { i2 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output).Result;

            Assert.AreEqual(1, renderedids.Count(), "Number of images that were rendered");
            Assert.AreEqual((ulong)1, renderedids.First(), "First image ID");

            output.Refresh();
            Assert.IsTrue(output.Exists, "dz file");

            // And the output file should contain both #1 and #2, in order.
            Approvals.Verify(output);
        }

        [TestMethod]
        [DeploymentItem("DZImage1", "DZImage1")]
        public async Task RenderCollectionFewUpdates()
        {
            var c = new RenderDZCollectionFromImages();
            var i1 = new FileInfo(@".\DZImage1\sc\vnpzyair.xml");
            var i2 = i1.GenerateNewDZFile("vnpzyair2.xml");

            var output = new FileInfo(@".\DZImage1\dz.xml"); // Relative output directory location matters
            if (output.Exists)
                output.Delete();
            output.Refresh();
            Assert.IsFalse(output.Exists);

            var renderedids = await c.RenderCollection(new FileInfo[] { i1, i2 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);

            var i3 = i1.GenerateNewDZFile("vnpzyair3.xml");

            //
            // Now, we are going to be sneaky and go in and delete a file or two behind the
            // guys back. Because we know the internal workings, we know that this shouldn't
            // be regenerated.
            //

            var outputDirectory = new DirectoryInfo(@".\DZImage1\dz_files\8");
            Assert.IsTrue(outputDirectory.Exists);
            foreach (var f in outputDirectory.EnumerateFiles())
            {
                f.Delete();
            }

            //
            // Run the rendering again.
            //

            renderedids = await c.RenderCollection(new FileInfo[] { i1, i3 },
                f => f.FullName.Substring(f.FullName.IndexOf(@"\sc\") + 1).Replace(@"\", "/"),
                output);

            //
            // How many new files showed up??
            //

            outputDirectory.Refresh();
            Assert.AreEqual(1, outputDirectory.EnumerateFiles().Count(), "# of output files");
        }
    }
}
